#include "lobbymodel.h"
#include "sdk/public/steam/steam_api.h"

LobbyModel::LobbyModel(QObject *parent)
    : QAbstractListModel{parent}
{}

int LobbyModel::rowCount(const QModelIndex &parent) const
{
    qDebug() << "LobbyModel::rowCount " << lobbyPlayers.length();
    return lobbyPlayers.length();
}

QHash<int, QByteArray> LobbyModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[NameRole] = "name";
    roles[StateRole] = "state";
    roles[SIDRole] = "sid";
    roles[IsHostRole] = "isHost";
    roles[IsSelfRole] = "isSelf";
    roles[SelfIsHostRole] = "selfIsHost";
    return roles;
}


QVariant LobbyModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    switch(role) {
    case NameRole:
        return QVariant(lobbyPlayers[index.row()].f_name);
    case StateRole:
        return QVariant(lobbyPlayers[index.row()].f_state);
    case SIDRole:
        return QVariant(QString("%1").arg(lobbyPlayers[index.row()].f_sid));
    case IsHostRole:
        return QVariant(lobbyPlayers[index.row()].f_host == 1);
    case IsSelfRole:
        return QVariant(lobbyPlayers[index.row()].f_sid == SteamUser()->GetSteamID().ConvertToUint64());
    case SelfIsHostRole:
        for (int i = 0; i < lobbyPlayers.size(); i++) {
            if (lobbyPlayers[i].f_host == 1) {
                qlonglong host_id = lobbyPlayers[i].f_sid;
                return QVariant(host_id == SteamUser()->GetSteamID().ConvertToUint64());
            }
        }
        return QVariant(false);
    default:
        return QVariant();
    }
}

void LobbyModel::refresh(QList<Friend> data)
{
    bool shouldRefresh = false;
    if (data.length() != lobbyPlayers.length()) {
        shouldRefresh = true;
        qDebug() << "lobby size changed";
    } else {
        for (int i = 0; i < data.size(); i++) {
            if (data[i].f_sid != lobbyPlayers[i].f_sid) {
                shouldRefresh = true;
                qDebug() << "lobby player changed";
                break;
            }
            if (data[i].f_state != lobbyPlayers[i].f_state) {
                shouldRefresh = true;
                qDebug() << "lobby player state";
                break;
            }
            if (data[i].f_host != data[i].f_host) {
                shouldRefresh = true;
                qDebug() << "lobby host changed";
                break;
            }
        }
    }
    if (shouldRefresh) {
        qDebug() << "Refreshing lobby players " << data.length();
        emit beginResetModel();
        lobbyPlayers.clear();
        for (int i = 0; i < data.length(); i++) {
            CSteamID friendSteamID;
            friendSteamID.SetFromUint64(data[i].f_sid);
            const char *friendName = SteamFriends()->GetFriendPersonaName( friendSteamID );
            Friend f;
            f.setName(QString(friendName));
            f.setSid(friendSteamID.ConvertToUint64());
            f.setHost(data[i].f_host);
            f.setState(data[i].f_state);
            lobbyPlayers.append(f);
        }
        emit endResetModel();
    }
}

void LobbyModel::kick_player(QString qsid) {
    qDebug() << "kick_player " << qsid;
    emit beginResetModel();
    if (lobbyPlayers.size() <= 2) {
        qDebug() << "Lobby size 2, remove all.";
        lobbyPlayers.clear();
    } else {
        for (int i = 0; i < lobbyPlayers.size(); i++) {
            if (lobbyPlayers[i].f_sid == qsid.toLongLong()) {
                qDebug() << "Removing player at idx " << i;
                lobbyPlayers.removeAt(i);
                break;
            }
        }
    }
    emit endResetModel();
}
